package com.zsc.edu.gateway.modules.message.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.framework.mybatisplus.DataPermission;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.message.dto.BulletinDto;
import com.zsc.edu.gateway.modules.message.entity.Bulletin;
import com.zsc.edu.gateway.modules.message.query.BulletinQuery;
import com.zsc.edu.gateway.modules.message.service.BulletinService;
import com.zsc.edu.gateway.modules.message.vo.BulletinVo;
import com.zsc.edu.gateway.modules.operationLog.entity.OperationLogAnnotation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告Controller
 *
 * @author harry_yao
 */
@AllArgsConstructor
@RestController
@RequestMapping("/api/rest/bulletin")
public class BulletinController {

    private final BulletinService service;
    /**
     * 普通用户查看公告详情
     *
     * @param id ID
     * @return 公告
     */
    @GetMapping("/self/{id}")
    public BulletinVo selfDetail(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("id") Long id) {
        return service.detail(userDetails,id, Bulletin.State.publish);
    }

    /**
     * 普通用户分页查询公告
     *
     * @param query    查询表单
     * @return 分页数据
     */
    @DataPermission
    @GetMapping("/self")
    public Page<Bulletin> getBulletins(Page<Bulletin> page, BulletinQuery query) {
        query.setState(Bulletin.State.publish);
        return service.page(page, query.wrapper());
    }

    /**
     * 管理查询公告详情
     *
     * @param id ID
     * @return 公告
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('message:bulletin:query')")
    public BulletinVo detail(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("id") Long id) {
        return service.detail(userDetails,id, null);
    }

    /**
     * 管理员分页查询公告
     *
     * @param query    查询参数
     * @return 分页数据
     */
    @DataPermission
    @GetMapping
    @PreAuthorize("hasAuthority('message:bulletin:query')")
    public Page<Bulletin> query(Page<Bulletin> page, BulletinQuery query) {
        return service.page(page, query.wrapper());
    }

    /**
     * 创建公告
     *
     * @param userDetails 操作用户
     * @param dto         表单数据
     * @return 公告
     */
    @PostMapping
    @PreAuthorize("hasAuthority('message:bulletin:create')")
    @OperationLogAnnotation(content = "'公告'", operationType = "新建")
    public Bulletin create(@AuthenticationPrincipal UserDetailsImpl userDetails, @RequestBody BulletinDto dto) {
        return service.create(userDetails, dto);
    }

    /**
     * 更新公告，只能修改"编辑中"或"已发布"的公告，"已发布"的公告修改后会改为"编辑中"
     *
     * @param userDetails 操作用户
     * @param dto         表单数据
     * @param id          ID
     * @return 公告
     */
    @PatchMapping("/{id}")
    @PreAuthorize("hasAuthority('message:bulletin:update')")
    @OperationLogAnnotation(content = "'公告'", operationType = "更新")
    public Boolean update(@AuthenticationPrincipal UserDetailsImpl userDetails, @RequestBody BulletinDto dto, @PathVariable("id") Long id) {
        return service.update(userDetails, dto, id);
    }

    /**
     * 切换公告置顶状态
     *
     * @param id ID
     * @return 公告
     */
    @PatchMapping("/{id}/toggle-top")
    @PreAuthorize("hasAuthority('message:bulletin:update')")
    @OperationLogAnnotation(content = "'公告置顶状态'", operationType = "更新")
    public Boolean toggleTop(@PathVariable("id") Long id) {
        return service.toggleTop(id);
    }

    /**
     * 发布公告，只能发布"编辑中"的公告
     *
     * @param userDetails 操作用户
     * @param ids          IDs
     * @return 公告
     */
    @PatchMapping("/publish")
    @PreAuthorize("hasAuthority('message:bulletin:update')")
    @OperationLogAnnotation(content = "'发布公告状态'", operationType = "更新")
    public Boolean publish(@AuthenticationPrincipal UserDetailsImpl userDetails, @RequestBody List<Long> ids) {
        return service.publish(userDetails, ids);
    }

    /**
     * 关闭公告，只能关闭"已发布"的公告
     *
     * @param userDetails 操作用户
     * @param id          ID
     * @return 公告
     */
    @PatchMapping("/{id}/toggleClose")
    @PreAuthorize("hasAuthority('message:bulletin:update')")
    @OperationLogAnnotation(content = "'关闭公告状态'", operationType = "更新")
    public Boolean toggleClose(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("id") Long id) {
        return service.close(userDetails, id);
    }

    /**
     * 删除公告，只能删除"编辑中"的公告
     *
     * @param id ID
     * @return 公
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('message:bulletin:delete')")
    @OperationLogAnnotation(content = "'公告'", operationType = "删除")
    public Boolean delete(@PathVariable("id") Long id) {
        return service.delete(id);
    }

}
